/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.hercules.android.receiver.AndroidReceiver;

/**
 * This activity is to be used by the application user to select a server to connect to.
 * <p>
 * This activity is entry point of application, user will be able to select server and then
 * {@link DisplayApplicationActivity} will load configuration data for selected server and create
 * {@link AndroidReceiver} using configurated properties.
 * <p>
 * <b>Required layout XML:</b>
 *
 * <pre>
 * &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 *   android:orientation="vertical" android:layout_width="fill_parent"
 *   android:layout_height="fill_parent"&gt;
 *
 *   &lt;TextView android:layout_width="fill_parent"
 *     android:layout_height="wrap_content" android:text="Please select a server"
 *     android:gravity="center" /&gt;
 *
 *   &lt;ListView android:id="@+id/server_list" android:layout_width="fill_parent"
 *     android:layout_height="wrap_content" android:choiceMode="singleChoice"
 *     android:entries="@array/servers" /&gt;
 *
 * &lt;/LinearLayout&gt;
 * </pre>
 *
 * <b>Sample resources XML:</b>
 *
 * <pre>
 *   &lt;string-array name="servers"&gt;
 *     &lt;item&gt;China:xre://localhost:80/chinaapp&lt;/item&gt;
 *     &lt;item&gt;USA:xre://localhost:80/usapp&lt;/item&gt;
 *     &lt;item&gt;UK:xre://localhost:80/ukapp&lt;/item&gt;
 *     &lt;item&gt;France:xre://localhost:80/franceapp&lt;/item&gt;
 *   &lt;/string-array&gt;
 * </pre>
 *
 * <b>Thread Safety:</b> This class is effectively thread safe.
 *
 * @author argolite, 7realm
 * @version 1.0
 */
public class SelectServerActivity extends Activity {
    /**
     * Default empty constructor.
     */
    public SelectServerActivity() {
        // empty
    }

    /**
     * Called when the activity is starting.
     *
     * @param savedInstanceState if the activity is being re-initialized after previously being shut down then this
     * Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle), otherwise it is null
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set view
        setContentView(R.layout.main);

        // add listener for button
        ListView listView = (ListView) findViewById(R.id.server_list);
        listView.setOnItemClickListener(new MyItemClickListener());
    }

    /**
     * Event handler for OnItemClick event.
     *
     * @author 7realm
     * @version 1.0
     */
    private final class MyItemClickListener implements OnItemClickListener {
        /**
         * Parses text of clicked item and if is correct, shows {@link DisplayApplicationActivity} activity.
         *
         * @param parent the AdapterView where the click happened
         * @param view the view within the AdapterView that was clicked (this will be a view provided by the adapter)
         * @param position the position of the view in the adapter
         * @param id the row id of the item that was clicked
         */
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // parse selected item
            String selected = parent.getItemAtPosition(position).toString();
            String[] parts = selected.split("\\:", 2);
            if (parts.length == 2) {
                // prepare intent
                Intent intent = new Intent();
                intent.setClassName(getPackageName(), DisplayApplicationActivity.class.getName());
                intent.putExtra(DisplayApplicationActivity.SERVER_ID, parts[0]);

                // start activity
                startActivity(intent);
            }
        }
    }
}